<?php

//include_once "CF_all_com.php";

// funciton to create a drop-down menu from an array of arrays, reflecting results of a quwery.
// $valuesarray is the array, formed by putting each mysql_fetch_array into a holder array
// $selectname is the name of the select box created
// $showkey is the key (string) of the string to put int he drop-down to have show up
// $valuekey is the key (string) of the data to put in the value = part
// $selectedvalue is the key (string) of the value to show as selected when form is created.
// $query it the query to get the text from
// $text limit is the max length of option text
// CT
//CT - 011308 - Added get_mysql_array funciton
// ct-010408 - Added do_query function
// ct-2008-01-15 - Added check_for_data function
// ct 2008-03-08 - Added DEBUGlookup_value, commented out 'no results for autotable query' in make_table 

function make_dropdown( $dd_name, $fieldname, $fieldkey, $defaultkey, $query, $textlimit = 80) {
	$get_elements_list = mysql_query($query) or die(mysql_error());

	//$selected_element = 1;
	if(mysql_num_rows($get_elements_list)) {
	$atindex = 1;
	while ($curr_element = mysql_fetch_array($get_elements_list)) {
		$elements_list[$atindex] = $curr_element;
		$atindex++;
		}

	}
	
else {
		$dropdowntext = "No results ";
		$dropdowntext .= $dd_name;
		$dropdowntext .= " query list /n";
		}
	

$dd_text = make_array_dd($elements_list, $dd_name, $fieldname, $fieldkey, $defaultkey, $textlimit);
return $dd_text;


}

function make_dd(& $db_pointer , $dd_name, $fieldname, $fieldkey, $defaultkey, $query, $textlimit = 80) {
	$get_elements_list = mysql_query($query, $db_pointer) or die(mysql_error());

	//$selected_element = 1;
	if(mysql_num_rows($get_elements_list)) {
	$atindex = 1;
	while ($curr_element = mysql_fetch_array($get_elements_list)) {
		$elements_list[$atindex] = $curr_element;
		$atindex++;
		}

	}
	
else {
		$dropdowntext = "No results ";
		$dropdowntext .= $dd_name;
		$dropdowntext .= " query list /n";
		}
	

$dd_text = make_array_dd($elements_list, $dd_name, $fieldname, $fieldkey, $defaultkey, $textlimit);
return $dd_text;


}

// ----------------------------------------------------------------Support Functions

//This function can be used to return a value from a table for use .
// $res_column should be a column name, $tablestring the table(or joined tables)  $whereclause is teh exact where clause after the 'Where'
function lookup_value ($res_column, $tablestring, $whereclause) {
	$query_lookup = "SELECT $res_column FROM $tablestring WHERE $whereclause LIMIT 1";
	$result = mysql_query($query_lookup) or ($value = "Error");
	//print $query_lookup . "\n" . mysql_error() . "\n";
	if($result) {
		$row = mysql_fetch_array($result);
		$value = $row[$res_column];
		}
	return $value;

}

function DEBUGlookup_value ($res_column, $tablestring, $whereclause) {
	$query_lookup = "SELECT $res_column FROM $tablestring WHERE $whereclause LIMIT 1";
	$result = mysql_query($query_lookup) or ($value = "Error");
	print $query_lookup . "\n" . mysql_error() . "\n";
	exit;
	if($result) {
		$row = mysql_fetch_array($result);
		$value = $row[$res_column];
		}
	return $value;

}

//This function can be used to return the maximum value (usefull for inserts when we cant do it all in one insert)
// $res_column should be a column name, $tablestring the table(or joined tables)  $whereclause is teh exact where clause after the 'Where'
function get_max_value ($res_column, $tablestring, $whereclause) {
	$query_lookup = "SELECT MAX($res_column) FROM $tablestring WHERE $whereclause LIMIT 1";
	$result = mysql_query($query_lookup) or ($value = "Error");
	//print $query_lookup . "  " . $value . "\n";
	if($result) {
		$row = mysql_fetch_array($result);
		$value = $row["MAX(".$res_column.")"];
		//$value=$row[1];
		}
		//print "\n Value is : $value";
	return $value;

}

//This function can be used to return the maximum value (usefull for inserts when we cant do it all in one insert)
// $res_column should be a column name, $tablestring the table(or joined tables)  $whereclause is teh exact where clause after the 'Where'
function get_query_count ($thequery) {
	$thequery = str_replace("\n", " ", $thequery);
		
	$thequery = preg_replace("/SELECT.+FROM/", "SELECT count(*) FROM", $thequery);

	$result = mysql_query($thequery) or ($value = 0);
	//print $query_lookup . "  " . $value . "\n";
	if($result) {
		$row = mysql_fetch_array($result);
		$value = $row["count(*)"];
		//$value=$row[1];
		}
		//print "\n Value is : $value";
	return $value;

}


//This function can be used to return a delimited list of values from a query.
// $res_column should be a column name, $tablestring the table(or joined tables)  $whereclause is teh exact where clause after the 'Where'
function get_value_list($query_lookup, $delimiter) {
	$get_results = mysql_query($query_lookup) or die(mysql_error());

	if(mysql_num_rows($get_results)) {
		$index = 0;
		while($resultsarray = mysql_fetch_array($get_results)) {
			if($index > 0) {
				$value .= $delimiter;
				}
			$value .=  $resultsarray[0] ;
			$index ++;
			}
		}
	
	
	return $value;

}


// This function checks if a value exist or not in the DB, returns 1 on data and 0 of empty set.
function check_for_data($query_lookup){
	$get_results = mysql_query($query_lookup) or die(mysql_error());

	if(mysql_num_rows($get_results)) {
		return 1;
		}
	else {
		return 0;
		}

}

function make_table($thequery, $showvalues, $keyvalues, $mode = "ro", $actionpage = "", $editpage = "", $referstring = "", $condense = 'none')
{
$get_results = mysql_query($thequery) or die(mysql_error());

	
if(mysql_num_rows($get_results)) {
$index = 1;
	while($resultsarray = mysql_fetch_assoc($get_results)) {
	$valuesarray[$index] = $resultsarray;
	$index ++;
		}
//	$displayblock = make_ele_disp_table($allresults, $tailoring_id);

	// Check for and execute one condense
		// IF values shluod be condensed, use this...(cusomizes function)
	if($condense != 'none') {
		if($condense != 'none') {
			$column = $condense['column'];
			$delimiter = $condense['delimiter'];
			$prefix = $condense['prefix'];
			$postfix = $condense['postfix'];
			$valuesarray = array_condense_col($valuesarray, $column, $delimiter, $prefix, $postfix );
		}
	}

	$table_text = make_array_table($valuesarray, $showvalues, $keyvalues, $mode, $actionpage, $editpage, $referstring);

	}
else {
		//$table_text .= "Error - no results for autotable query" ;
}

return $table_text;

}

// This function wil form a csv version of the table
// $thename is the name of the file (filename is name + .csv) !! Added braks old compatability
// $query is the query.
// $showvalues is an array of key = column, assign to 1 to show.
// $condense = array as follows to describe condensing on a row.
// ['column'] = column to condense; ['delimiter'] = delimiter to use; ['prefix'] = prefix to use; ['postfix'] = postfix to use
// ct 071307
function make_csv_table($thename, $thequery, $showvalues, $condense = 'none')
{
	$get_results = mysql_query($thequery) or die(mysql_error());
	$table_text = "";

	if(mysql_num_rows($get_results)) {
		$index = 1;
		while($resultsarray = mysql_fetch_assoc($get_results)) {
			$valuesarray[$index] = $resultsarray;
			$index ++;
			}

		}
	else {
		$csv_text .= "No results for query" ;
	}

	//$csv_text = "";
	make_csv_array_table($thename, '' , $valuesarray, $showvalues);
}

function get_mysql_array($thequery){
	$get_results = mysql_query($thequery) or die(mysql_error());
	
	if(mysql_num_rows($get_results)) {
		$index = 1;
		while($resultsarray = mysql_fetch_assoc($get_results)) {
			$valuesarray[$index] = $resultsarray;
			$index ++;
			}
		return $valuesarray;
		}
}

//GENERIC WRAPPER FUNCTION for Query Exec
function do_query($query) {
	$result = mysql_query($query) or die(mysql_error());
	return $result;
}

function get_array_query($thequery){
	$get_results = mysql_query($thequery) or die(mysql_error());
	
	if(mysql_num_rows($get_results)) {
		$index = 1;
		while($resultsarray = mysql_fetch_assoc($get_results)) {
			$valuesarray[$index] = $resultsarray;
			$index ++;
			}
		return $valuesarray;
		}

}

function get_array_limitquery($thequery, $start = 0, $limit = 'MAX'){
	if($limit == 'MAX'){
		$get_results = mysql_query($thequery) or die(mysql_error());
	}else{
		$get_results = mysql_query($thequery . " LIMIT $start, $limit ") or die(mysql_error());
	}
	if(mysql_num_rows($get_results)) {
		$index = 1;
		while($resultsarray = mysql_fetch_assoc($get_results)) {
			$valuesarray[$index] = $resultsarray;
			$index ++;
			}
		return $valuesarray;
		}

}

?>
